<!--
 * @Descripttion: 
 * @version: 
 * @Author: Jun
 * @Date: 2021-06-04 14:26:06
 * @LastEditors: Jun
 * @LastEditTime: 2021-06-04 17:06:00
-->
<template>
  <div class="el-form-list">
    <div v-for="item of publicForm" :key="item.id">
      <!-- 输入框 -->
      <template v-if="item.type === 1">
        <el-form-item :label="item.ndsib">
          <my-input v-model="item.value"></my-input>
        </el-form-item>
      </template>

      <!-- 单选框 -->
      <template v-if="item.type === 2">
        <el-form-item :label="item.ndsib">
          <my-radio :valueArray="item.options" v-model="item.value"></my-radio>
        </el-form-item>
      </template>

      <!-- 多选框 -->
      <template v-if="item.type === 3">
        <el-form-item :label="item.ndsib">
          <my-check-box
            :valueArray="item.options"
            v-model="item.value"
          ></my-check-box>
        </el-form-item>
      </template>

      <!-- 下拉列表 -->
      <template v-if="item.type === 4">
        <el-form-item :label="item.ndsib">
          <my-select
            :valueArray="item.options"
            v-model="item.value"
          ></my-select>
        </el-form-item>
      </template>

      <!-- 上传文件 -->
      <template v-if="item.type === 5">
        <el-form-item :label="item.ndsib">
          <upload v-model="item.value"></upload>
        </el-form-item>
      </template>

      <!-- 日期 -->
      <template v-if="item.type === 6">
        <el-form-item :label="item.ndsib">
          <date-picker v-model="item.value"></date-picker>
        </el-form-item>
      </template>

      <!-- 多行文本 -->
      <template v-if="item.type === 7">
        <el-form-item :label="item.ndsib">
          <my-input v-model="item.value" type="textarea"></my-input>
        </el-form-item>
      </template>
    </div>
  </div>
</template>

<script>
import { updateUrl } from "api/userInfo.js";
import {setFormVal} from "../utils/setFormVal"
import {setFormOptions} from "../utils/setFormOptions"
import { createNamespacedHelpers } from "vuex";
const { mapMutations } = createNamespacedHelpers("userInfo");
export default {
  props: {
    form: {
      //表单
      type: Array,
      required: true,
    },
    userInfo: {
      //用户信息
      type: Object,
      required: true,
    },
  },
  data() {
    return {
      publicForm: "", //公共表单
    };
  },
  methods: {
    ...mapMutations(["updateUserInfo"]),
    API_update(data) {
      return this.$httpRequest({
        url: updateUrl,
        method: "POST",
        data: {
          data,
        },
      });
    },
    async update() {//更新用户信息
      let userInfo = JSON.parse(JSON.stringify(this.userInfo));
      this.publicForm.forEach((item) => {
        for (let key in userInfo) {
          if (key === item.name) {
            userInfo[key] = item.value;
          }
        }
      });

      let resUserInfo = await this.API_update(userInfo); //更新用户信息
      if (resUserInfo.data.code === 1000) {
        this.updateUserInfo(userInfo);
        this.$message.success(resUserInfo.data.msg);
      } else {
        this.$message.error(resUserInfo.data.msg);
      }
    },
  },
  created() {
    this.publicForm = this.form.filter((item) => {
      //过滤出公共表单
      return item.ispublic === 1;
    });

    setFormVal(this.publicForm);
    setFormOptions(this.publicForm);

    // 用户信息和公共表单进行比对，然后赋值
    this.publicForm.forEach((item) => {
      if (this.userInfo[item.name]) {
        item.value = this.userInfo[item.name];
      }
    });
  },
};
</script>

<style>
</style>